package netfone;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;
import au.com.bytecode.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.commons.lang.time.DateUtils;

public class Parser {
    
   
    

    static SimpleDateFormat formate = new SimpleDateFormat("dd/MM/yy hh:mm:ss");
    public static List<Call> simpleParse(String filePath) throws IOException {
        List<Call> calls = new ArrayList<Call>();
        CSVReader reader = new CSVReader(new FileReader(filePath));
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            if (nextLine.length > 0) {
                if (isLineValid(nextLine)) {
                    // linha com design valido
                    Call call = new Call();
                    String time = nextLine[7];
                    if (time != null && !time.trim().equals("")) {
                        try {
                            String column = nextLine[3];
                            String theDate = column.split("A")[0];
                            String cleanDate = StringUtils.remove(theDate, "\"").trim();
                            Date date = date = formate.parse(cleanDate + " " +  time);
                            call.setDate(date);
                        } catch (ParseException ex) {
                            Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    call.setDialedNumber(nextLine[4].trim());
                    call.setDestination(nextLine[6].trim());
                    call.setCallCost(new Double(nextLine[13]));
                    calls.add(call);
                }

            }
        }

        return calls;

    }

    private static boolean isLineValid(String[] nextLine) {

        if (nextLine.length >= 14) {
            String vDouble = StringUtils.remove(nextLine[13], '.');
            boolean isDouble = StringUtils.isNumericSpace(vDouble);
            boolean isNumber = StringUtils.isNumericSpace(nextLine[4]);
            return (isDouble && isNumber);
        }
        return false;
    }
}
